home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / texture / mipmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  8.0 KB  |  342 lines

  1. /*
  2.  * Copyright 1993, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* mipmap.c
  19.  * This program reads in a texture from a .rgb file, uses the utility
  20.  * routine gluBulid2dMipmaps() to scale and load mipmaps for a single 
  21.  * texture image, then applies the texture to a rectangular polygon
  22.  * using explicit texture coordinates.  If no file is given, the 
  23.  * program creates its own solid color mipmaps.  The user can 
  24.  * switch between the various minification and magnification filters.
  25.  *
  26.  *    <a> key        - pause/restart movement
  27.  *    <m> key        - cycle minification filter
  28.  *    <M> key        - cycle Magnification filter
  29.  *    <R> key        - reset image to starting position
  30.  *    Escape Key    - exit program
  31.  */
  32. #include <GL/gl.h>
  33. #include <GL/glu.h>
  34. #include <GL/glut.h>
  35.  
  36. #include <math.h>
  37. #include <stdio.h>
  38.  
  39. #include "rgbImageFile.h"    /* should be in ../../include directory */
  40.  
  41. /*  Function Prototypes  */
  42.  
  43. GLvoid  initgfx( GLvoid );
  44. GLvoid  animate( GLvoid );
  45. GLvoid  visibility( GLint );
  46. GLvoid  drawScene( GLvoid );
  47. GLvoid  reshape( GLsizei, GLsizei );
  48. GLvoid  keyboard( GLubyte, GLint, GLint );
  49.  
  50. GLvoid  initTexture( unsigned int *, GLsizei, GLsizei );
  51. GLvoid  cycleMinFilter( GLvoid );
  52. GLvoid  cycleMagFilter( GLvoid );
  53. GLvoid  toggleMoving( GLvoid );
  54. GLvoid  resetSwim( GLvoid );
  55.  
  56. void  printFilters( GLvoid );
  57. void  printHelp( char *progname );
  58.  
  59. /* Global Definitions */
  60.  
  61. #define KEY_ESC    27    /* ascii value for the escape key */
  62.  
  63. #define MAXDIM 128    /* Maximum texture size */
  64.  
  65. /* Global Variables */
  66.  
  67. static GLboolean moving = GL_TRUE;
  68. static GLfloat swim = 1.0;
  69.  
  70. typedef struct {
  71.     GLint    type;
  72.     char    *name;
  73. } FilterInfo;
  74.  
  75. static FilterInfo     filters[6] = {
  76.     { GL_NEAREST, "GL_NEAREST" },
  77.     { GL_LINEAR, "GL_LINEAR" },
  78.     { GL_NEAREST_MIPMAP_NEAREST, "GL_NEAREST_MIPMAP_NEAREST" },
  79.     { GL_LINEAR_MIPMAP_NEAREST, "GL_LINEAR_MIPMAP_NEAREST" },
  80.     { GL_NEAREST_MIPMAP_LINEAR, "GL_NEAREST_MIPMAP_LINEAR" },
  81.     { GL_LINEAR_MIPMAP_LINEAR, "GL_LINEAR_MIPMAP_LINEAR" } 
  82. };
  83.  
  84. static int curMagFilter = 0;
  85. static int curMinFilter = 0;
  86.  
  87. void
  88. main( int argc, char *argv[] )
  89. {
  90.     GLsizei     width, height;
  91.     char        *imageFileName = "fish.rgba";
  92.     unsigned int     *image;
  93.     GLsizei        imageWidth = 0, imageHeight = 0;
  94.  
  95.     glutInit( &argc, argv );
  96.  
  97.     if (argc > 2) {
  98.         fprintf (stderr, "usage: %s [filename]\n", argv[0]);
  99.         exit(-1);
  100.     } else if (argc == 2) {
  101.         imageFileName = argv[1];
  102.         fprintf(stdout, "using image %s\n", imageFileName );
  103.         image = rgbReadImageFile(imageFileName, &imageWidth,
  104.              &imageHeight);
  105.     }
  106.  
  107.     /* create a window that is 1/4 the size of the screen */
  108.  
  109.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  110.     height = glutGet( GLUT_SCREEN_HEIGHT );
  111.     glutInitWindowPosition( (width / 2) + 4, height / 4 );
  112.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  113.     glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
  114.     glutCreateWindow( argv[0] );
  115.  
  116.     initTexture( image, imageWidth, imageHeight );
  117.     initgfx();
  118.  
  119.     glutKeyboardFunc( keyboard );
  120.     glutIdleFunc( animate );
  121.     glutVisibilityFunc( visibility );
  122.     glutReshapeFunc( reshape );
  123.     glutDisplayFunc( drawScene ); 
  124.  
  125.     printHelp( argv[0] );
  126.  
  127.     glutMainLoop();
  128. }
  129.  
  130. GLvoid
  131. printHelp( char *progname )
  132. {
  133.     fprintf(stdout, "\n%s - demonstrates texture map filtering \n"
  134.         "<a> key    - pause/restart movement\n"
  135.         "<m> key    - cycle minification filter\n"
  136.         "<M> key    - cycle Magnification filter\n"
  137.         "<R> key    - reset image to starting position\n"
  138.         "Escape key     - exit the program\n\n",
  139.         progname );
  140. }
  141.  
  142. GLvoid
  143. initgfx( void )
  144. {
  145.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  146. }
  147.  
  148. GLvoid 
  149. initTexture( unsigned int *image, GLsizei imageWidth, GLsizei imageHeight )
  150. {
  151.     GLint     r, g, b;
  152.     int    i, dim;
  153.     GLint    level;
  154.     GLubyte    *mipmap;
  155.  
  156.     if (imageWidth != 0 && imageHeight != 0 ) {
  157.         /* scale texture image, make mipmaps and load texture
  158.          *    gluBuild2DMipmaps( target, components, width, height,
  159.          *        format,  type, imageArray ) 
  160.          */
  161.         gluBuild2DMipmaps(GL_TEXTURE_2D, 4, imageWidth, imageHeight,
  162.              GL_RGBA, GL_UNSIGNED_BYTE, image);
  163.     } else {
  164.         /* build artificial colored mipmaps to show the changes */
  165.  
  166.         fprintf (stdout, "creating artificial mipmaps\n");
  167.  
  168.         dim = MAXDIM;
  169.         for (level = 0; dim >= 1; level++, dim >>= 1 ) {
  170.  
  171.             mipmap = (GLubyte *) malloc(dim*dim*4*sizeof(GLubyte));
  172.             
  173.             b = level & 0x1;
  174.             g = (level>>1) & 0x1; 
  175.             r = (level>>2) & 0x1; 
  176.             r=!r; g=!g; b=!b;
  177.             for ( i = 0; i < dim * dim * 4; ) {
  178.                 mipmap[i++] = r * 0xff;
  179.                 mipmap[i++] = g * 0xff;
  180.                 mipmap[i++] = b * 0xff;
  181.                 mipmap[i++] = 0;
  182.             }
  183.  
  184.             printf("mipmap %d size is %3d X %3d, "
  185.                 "color is (%d, %d, %d, %d)\n",
  186.                 level, dim, dim, r, g, b, 0);
  187.  
  188.             /* use the image as a texture */
  189.  
  190.             glTexImage2D(GL_TEXTURE_2D, level, 4, dim, dim,
  191.                 0, GL_RGBA, GL_UNSIGNED_BYTE, mipmap);
  192.         }
  193.     }
  194.     
  195.     glEnable(GL_TEXTURE_2D);
  196.  
  197.     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
  198.             filters[curMinFilter].type );
  199.     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, 
  200.             filters[curMagFilter].type );
  201.     printFilters();
  202. }
  203.  
  204.  
  205. GLvoid
  206. cycleMinFilter( GLvoid )
  207. {
  208.     curMinFilter = (curMinFilter +1) % 6;
  209.     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
  210.             filters[curMinFilter].type );
  211.     printFilters();
  212. }
  213.             
  214.  
  215. GLvoid
  216. cycleMagFilter( GLvoid )
  217. {
  218.     curMagFilter = (curMagFilter +1) % 2;
  219.     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, 
  220.             filters[curMagFilter].type );
  221.     printFilters();
  222. }
  223.  
  224.  
  225. GLvoid
  226. printFilters( GLvoid )
  227. {
  228.     printf("mag filter: %10s,   min filter: %26s\n", 
  229.         filters[curMagFilter].name, filters[curMinFilter].name); 
  230. }
  231.  
  232.  
  233. GLvoid
  234. resetSwim ( GLvoid )
  235. {
  236.     swim = 1.0;
  237. }
  238.  
  239.  
  240. GLvoid
  241. toggleMoving ( GLvoid )
  242. {
  243.     moving = ! moving;
  244.     printf ("%s\n", moving?"...MOVING":"PAUSED...");
  245.  
  246.     if (moving) {
  247.         glutIdleFunc( animate );
  248.     } else {
  249.         glutIdleFunc( NULL );
  250.     }
  251. }
  252.  
  253. GLvoid 
  254. keyboard( GLubyte key, GLint x, GLint y )
  255. {
  256.     switch (key) {
  257.     case 'a':    /* toggle animation */
  258.         toggleMoving();
  259.         break;
  260.     case 'm':          /* cycle minification filter */
  261.         cycleMinFilter();
  262.         break;
  263.     case 'M':          /* cycle Magnification filter */
  264.         cycleMagFilter();
  265.         break;
  266.     case 'R':          /* reset image to starting position */
  267.         resetSwim();
  268.         break;
  269.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  270.         exit(0);
  271.     }
  272.     glutPostRedisplay();
  273. }
  274.  
  275.  
  276. GLvoid
  277. reshape( GLsizei width, GLsizei height )
  278. {
  279.     GLfloat     aspect;
  280.     glViewport( 0, 0, width, height );
  281.  
  282.     aspect = (GLfloat) (width) / height;
  283.  
  284.     glMatrixMode( GL_PROJECTION );
  285.     glLoadIdentity();
  286.     gluPerspective( 30.0, aspect, 1.0, 1500.0 );
  287.     glMatrixMode( GL_MODELVIEW );
  288.     glLoadIdentity();
  289.     glTranslatef( 0.0, 0.0, -3.0 ); 
  290. }
  291.  
  292. GLvoid 
  293. animate( GLvoid )
  294. {
  295.     swim = fmodf( swim + 0.1, 35.0 );
  296.  
  297.     /* Tell GLUT to redraw the scene */
  298.     glutPostRedisplay();
  299. }
  300.  
  301. GLvoid
  302. visibility( int state ) 
  303. {
  304.     if (state == GLUT_VISIBLE && moving) {
  305.         glutIdleFunc( animate );
  306.     } else {
  307.         glutIdleFunc( NULL );
  308.     }
  309. }
  310.  
  311. GLvoid
  312. drawScene( GLvoid )
  313. {
  314.     static float v0[3] = { -1.5, -1.0, 0.0 };
  315.     static float v1[3] = {  1.5, -1.0, 0.0 };
  316.     static float v2[3] = {  1.5,  1.0, 0.0 };
  317.     static float v3[3] = { -1.5,  1.0, 0.0 };
  318.  
  319.     static float t0[2] = { 0.0, 0.0 };
  320.     static float t1[2] = { 1.0, 0.0 };
  321.     static float t2[2] = { 1.0, 1.0 };
  322.     static float t3[2] = { 0.0, 1.0 };
  323.  
  324.     glClear( GL_COLOR_BUFFER_BIT );
  325.  
  326.     glColor4f( 1.0, 1.0, 1.0, 1.0 );
  327.     glPushMatrix();
  328.         glTranslatef( -3+swim, 0.2*sinf(swim * 3.0), -swim*5.0 );
  329.     
  330.         glRotatef( 45.0, 0.0, 1.0, 0.0 );
  331.         glBegin( GL_QUADS );
  332.             glTexCoord2fv( t0 ); glVertex3fv( v0 );
  333.             glTexCoord2fv( t1 ); glVertex3fv( v1 );
  334.             glTexCoord2fv( t2 ); glVertex3fv( v2 );
  335.             glTexCoord2fv( t3 ); glVertex3fv( v3 );
  336.         glEnd();
  337.  
  338.     glPopMatrix();
  339.  
  340.     glutSwapBuffers();
  341. }
  342.